package com.ljfiction.book.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ljfiction.book.Baner.FistHom;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.MainActivity;
import com.ljfiction.book.activitys.SearchActivity;
import com.ljfiction.book.adapter.MyViewPagerAdapter;
import com.ljfiction.book.fragment.ranking.RankingFrament;
import com.ljfiction.book.tools.AppUtils;
import com.ljfiction.book.tools.EventTool;
import com.ljfiction.book.tools.ScreenUtils;
import com.umeng.analytics.MobclickAgent;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class HomeFragment extends BaseFragment  {
    private LinearLayout daohanglan_hom;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private MyViewPagerAdapter viewPagerAdapter;
    //TabLayout标签
    private String[] titles = new String[]{"热门", "分类", "排行"};
    private List<Fragment> fragments = new ArrayList<>();
    private Context context;
    private HomHotFragment homHotFragment;
    private BiaoqianFragment biaoqianFragment;
    private RankingFrament rankingFrament;
    private LinearLayout topbar;
    private FistHom f;
    private ImageView searchImagview;
    private Intent intent;
    private boolean isfirst=false;

    public static HomeFragment newInstance(FistHom f,boolean isfirst) {
        HomeFragment newFragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("fistHom",f);
        bundle.putBoolean("isfirst",isfirst);
        newFragment.setArguments(bundle);
        return newFragment;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            f= (FistHom) args.getSerializable("fistHom");
            isfirst=args.getBoolean("isfirst");
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
    }

    @Override
    @Nullable
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        daohanglan_hom = (LinearLayout) view.findViewById(R.id.daohanglan_hom);
        tabLayout = (TabLayout) view.findViewById(R.id.tab_layou3);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager);
        topbar = (LinearLayout) view.findViewById(R.id.toptab);
        searchImagview=view.findViewById(R.id.search_hom);
        context = getActivity();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) daohanglan_hom.getLayoutParams();
        //获取当前控件的布局对象
        params.height = MainActivity.getStatusBarHeight(context);//设置当前控件布局的高度
        daohanglan_hom.setLayoutParams(params);//将设置好的布局参数应用到控件中
        init();

        return view;
    }


    @SuppressLint("ClickableViewAccessibility")
    private void init() {
        //设置TabLayout标签的显示方式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //循环注入标签
        for (String tab : titles) {
            tabLayout.addTab(tabLayout.newTab().setText(tab));
        }
        //设置TabLayout点击事件
      /*  tabLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int postion = (int) v.getTag();
                switch (postion){
                    case 1:
                        EventTool.putEvent(context,"点击","点击","1_classClick");
                        break;
                    case 2:
                        EventTool.putEvent(context,"点击","点击","2_rankClick");
                        break;
                }
                return true;
            }
        });*/
      /* tabLayout.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
             int postion = (int) view.getTag();
               switch (postion){
                   case 1:
                       EventTool.putEvent(context,"点击","点击","1_classClick");
                       break;
                   case 2:
                       EventTool.putEvent(context,"点击","点击","2_rankClick");
                       break;
               }


           }
       });*/

        homHotFragment =HomHotFragment.newInstance(f,isfirst);
        biaoqianFragment = new BiaoqianFragment();
        rankingFrament = new RankingFrament();
        fragments.add(homHotFragment);
        fragments.add(biaoqianFragment);
        fragments.add(rankingFrament);
        viewPagerAdapter = new MyViewPagerAdapter(getChildFragmentManager(), titles, fragments);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setOffscreenPageLimit(2);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout);
            }
        });
        searchImagview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent=new Intent(context, SearchActivity.class);
                startActivity(intent);
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 1:
                        EventTool.putEvent(context,"点击","点击","1_classClick");
                        break;
                    case 2:
                        EventTool.putEvent(context,"点击","点击","2_rankClick");
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

  /*  @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
       switch (tab.getPosition()){
           case 1:
               EventTool.putEvent(context,"点击","点击","1_classClick");
               break;
           case 2:
               EventTool.putEvent(context,"点击","点击","2_rankClick");
               break;
       }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }
*/
    public void setIndicator(TabLayout tabs) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        /*int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());*/

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            if (i == 1) {
                params.leftMargin = ScreenUtils.getNum(context,80);
                params.rightMargin = ScreenUtils.getNum(context,80);
            } else {
                params.leftMargin = ScreenUtils.getNum(context,14);
                params.rightMargin = ScreenUtils.getNum(context,14);
            }
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

public  void tohotlist(){
        if (homHotFragment!=null){
            homHotFragment.refreshhotlist();
        }
}

    @Override
    public void onResume() {
        topbar.getBackground().setAlpha(255);
        super.onResume();
    }
}
